package com.wythe.mall.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wythe.mall.R;
import com.wythe.mall.intf.OnRecyclerViewItemClickListener;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.utils.DPIUtil;
import com.wythe.mall.utils.DensityUtils;

import java.util.List;


/**
 * Created by sm on 2017/11/29 0029.
 */

public class HomeShopHListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<ShopBean> mList;
    private int screenWidth;

    public HomeShopHListAdapter(Context context, List<ShopBean> list) {
        this.context = context;
        mList = list;
        screenWidth = DensityUtils.screenWidth(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_shop_h, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ShopBean item = mList.get(position);
        final ItemViewHolder itemHolder = (ItemViewHolder) holder;
        View rootView = itemHolder.itemView.getRootView();
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) rootView.getLayoutParams();
        params.width = (int) ((screenWidth - DPIUtil.dip2px(context,30)) / 2.5);
        rootView.setLayoutParams(params);
        Glide.with(context).load(item.getIcon()).error(R.drawable.personal_icon_default_avatar).into(itemHolder.mIvShop);

        itemHolder.mTvPrice.setText("￥" + item.getSkuVos().get(0).getPrice());
        itemHolder.mTvOrginPrice.setText("￥" + item.getSkuVos().get(0).getOriginalPrice());
        itemHolder.mTvOrginPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
        itemHolder.mTvDes.setText(item.getTitle());
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRecyclerViewItemClickListener != null) {
                    int layoutPosition = itemHolder.getLayoutPosition();
                    mOnRecyclerViewItemClickListener.onRecyclerViewItemClick(v, layoutPosition);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIvShop, mIvCar;
        private TextView mTvDes, mTvPrice,mTvOrginPrice;

        public ItemViewHolder(View itemView) {
            super(itemView);
            mIvShop = (ImageView) itemView.findViewById(R.id.iv_shop);
            mIvCar = (ImageView) itemView.findViewById(R.id.iv_car);
            mTvDes = (TextView) itemView.findViewById(R.id.tv_des);
            mTvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            mTvOrginPrice = itemView.findViewById(R.id.tv_orgin_price);
        }
    }

    private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener;

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        mOnRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }
}
